/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Notification.h"
#include "cJSON.h"
REGISTER_PLUGIN_CLASS(Notification)

bool Notification::execute(const string&  action, cJSON* args, CallbackContext cbc)
{
    if(action == "onArKTsResult") {
        return onArKTsResult(args);
    }
    if(action == "alert" || action == "confirm"|| action == "prompt") {
        m_cbc = cbc;
        string strMessage = cJSON_GetArrayItem(args, 0)->valuestring;
        string strTitle =  cJSON_GetArrayItem(args, 1)->valuestring;
        cJSON* pButtonJson = cJSON_GetArrayItem(args, 2);
        string strButtonLabel = "";
        if(pButtonJson->type == cJSON_String) {
            strButtonLabel = pButtonJson->valuestring;
        } else {
            char* pStrButton = cJSON_Print(pButtonJson);
            strButtonLabel = pStrButton;
            free(pStrButton);
        }
        string strDefault = "";
        if(action == "prompt" && cJSON_GetArraySize(args) >= 4) {
            strDefault = cJSON_GetArrayItem(args, 3)->valuestring;
        }
        
        cJSON* json = cJSON_CreateObject();
        cJSON_AddStringToObject(json, "message", strMessage.c_str());
        cJSON_AddStringToObject(json, "title", strTitle.c_str());
        cJSON_AddStringToObject(json, "buttonLabel", strButtonLabel.c_str());
        cJSON_AddStringToObject(json, "action", action.c_str());
        cJSON_AddStringToObject(json, "defaultText", strDefault.c_str());
        char* pPageArgs = cJSON_Print(json); 
        executeArkTs("./AlertDialog/NotificationAlert/NotificationAlert", 0, pPageArgs, "Notification", cbc);
        free(pPageArgs);
        cJSON_Delete(json);
        return true;
    }
    return true;
}
bool Notification::onArKTsResult(cJSON* args)
{
    string content = cJSON_GetObjectItem(args, "content")->valuestring;
    if(content == "alert") {
        PluginResult pluginResult(PluginResult::Status::OK, 0);
        m_cbc.sendPluginResult(pluginResult);
    }
    
    if(content == "confirm") {
        cJSON* json = cJSON_GetObjectItem(args, "result");
        if(json->type == cJSON_Array) {
            if(cJSON_GetArraySize(json) >= 2) {
                cJSON* jsonStrButton = cJSON_GetArrayItem(json, 0);
                cJSON* jsonButtons = cJSON_Parse(jsonStrButton->valuestring);
                
                cJSON* jsonSelButton = cJSON_GetArrayItem(json, 1);
                string strSelButton = jsonSelButton->valuestring;

                if(jsonButtons != NULL) {
                    for(int i=0; i<cJSON_GetArraySize(jsonButtons); i++) {
                        string strButton = cJSON_GetArrayItem(jsonButtons, i)->valuestring;
                        if(strButton == strSelButton) {
                            PluginResult pluginResult(PluginResult::Status::OK, i+1);
                            m_cbc.sendPluginResult(pluginResult);
                            break;
                        }
                    }
                    cJSON_Delete(jsonButtons);
                }
            }
        }
    }
    
    if(content == "prompt") {
        cJSON* json = cJSON_GetObjectItem(args, "result");
        if(json->type == cJSON_Array) {
            if(cJSON_GetArraySize(json) >= 2) {
                cJSON* jsonStrButton = cJSON_GetArrayItem(json, 0);
                cJSON* jsonButtons = cJSON_Parse(jsonStrButton->valuestring);
                
                cJSON* jsonSelButton = cJSON_GetArrayItem(json, 1);
                string strSelButton = jsonSelButton->valuestring;

                cJSON* jsonText = cJSON_GetArrayItem(json, 2);
                string strText = jsonText->valuestring;
                
                if(jsonButtons != NULL) {
                    for(int i=0; i<cJSON_GetArraySize(jsonButtons); i++) {
                        string strButton = cJSON_GetArrayItem(jsonButtons, i)->valuestring;
                        if(strButton == strSelButton) {
                            cJSON* pRet = cJSON_CreateObject();
                            cJSON_AddNumberToObject(pRet, "buttonIndex", i+1);
                            cJSON_AddStringToObject(pRet, "input1", strText.c_str());
                            PluginResult pluginResult(PluginResult::Status::OK, pRet);
                            m_cbc.sendPluginResult(pluginResult);
                            cJSON_Delete(pRet);
                            break;
                        }
                    }
                    cJSON_Delete(jsonButtons);
                }
            }
        }
    }
    
    return true;
}